package client;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.bind.DatatypeConverter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTTPSClientSimple {

    // Disable the hostname verification for demo purpose
    static {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
    }

    public static void main(String[] args){
        // Initialize configuration
        String path =System.getProperty("user.dir") + "/src/main/resources/server.jks";
        System.setProperty("javax.net.ssl.trustStoreType", "jks");
        System.setProperty("javax.net.ssl.trustStore", path);

        try{
            URL url = new URL("https://127.0.0.1:8081/coordinates");
            HttpsURLConnection client =(HttpsURLConnection) url.openConnection();
            client.setRequestProperty("Content-Type", "application/json");
            String encoding = DatatypeConverter.printBase64Binary("root3:123456".getBytes("UTF-8"));
            client.setRequestProperty("Authorization", "Basic " + encoding);

            System.out.println("RETURN : "+client.getResponseCode());
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
}